<?php
namespace app\index\controller;

use app\Logined;
use Endroid\QrCode\QrCode;
use think\facade\Config;
use think\facade\Db;

class User extends Logined
{
    public function user_info()
    {
        $msg = '';

        // 代理绑定团长
        if(!empty($this->param['leader_id'])){
            $leader_info = Db::name('user')->where('user_id', $this->param['leader_id'])->find();
            if($this->user_info['share_id']){
                $msg = '您已经绑定团长';
            } elseif($leader_info['is_leader'] == 0) {
                $msg = '对方不是团长，无权设置代理';
            } else {
                $msg = '绑定团长成功';
                Db::name('user')->where('user_id', $this->user_info['user_id'])->update([
                    'share_id'  =>  $this->param['leader_id'],
                    'is_agent'  =>  1
                ]);
            }
        }

        // 绑定分享人
        if(!empty($this->param['share_id'])){
            if($this->user_info['share_id']) {
                $msg = '您已经绑定上级';
            } else {
                $msg = '接受邀请成功';
                Db::name('user')->where('user_id', $this->user_info['user_id'])->update([
                    'share_id'  =>  $this->param['share_id']
                ]);
            }
        }

        return $this->ajax_return(1, $msg, [
            'user_info'     =>  $this->user_info
        ]);
    }

    public function qrcode()
    {
        $qrcode = new QrCode();
        $qrcode->setData('/pages/index/index?leader_id='.$this->user_info['user_id']);
        echo $qrcode->getData();

    }

}
